<template>
  <div>

    <el-form label-width="120px">

      <el-form-item label="信息描述">
          <el-tag type="info">
            excel模板说明
          </el-tag>
          <el-tag>
            <i class="el-icon-download"/>
            <a :href="'/static/01.xlsx'">下载模板</a>
          </el-tag>
      </el-form-item>
     <el-form-item label="信息描述">
      <el-upload
        class="upload-demo"
        ref="upload"
        :action="BASE_API+'/eduservice/subject/addSubject'"
        :before-upload="beforeUploadSinger"
        :auto-upload="false"
        :on-success="successupload"
        :limit="1"
        accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
        >
        <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
        <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传到服务器</el-button>
        <div slot="tip" class="el-upload__tip">只能上传xlsx文件，且不超过1M</div>
      </el-upload>
      </el-form-item>
       </el-form>
  </div>
</template>

<script>
  export default {
    data(){
      return {
        BASE_API:process.env.BASE_API,
      }
    },
    mounted() {

    },
    methods:{
      submitUpload() {
         this.$refs.upload.submit();
      },
      beforeUploadSinger(file){
        console.log(file)
      	const isJpg = (file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
      	if(!isJpg){
      		this.$message.error('上传xlsx')
      		return false;
      	}
      	const islt2M = (file.size/1024/1024)<2
      	if(!islt2M){
      		this.$message.error('上传文件不能超过2M')
      		return false;
      	}
      	return true;
      },
      successupload(){
        this.$notify({
               title: '成功',
               message: '上传成功',
               type: 'success'
        });
        this.$router.push("/subject/table")
      }
    }
  }
</script>

<style scoped>

</style>
